package com.fingard.dsp.bank.directbank.ccb04.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * 加解密工具类，提供标准的3DES实现
 * 
 * 
 */
public abstract class Des3 {
	private static final String Algorithm = "DESede"; // 定义 加密算法,可用
	// DES,DESede,Blowfish
	// keybyte为加密密钥，长度为24字节
	// src为被加密的数据缓冲区（源）
	public static byte[] encryptMode(byte[] keybyte, byte[] src) {
		try {
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
			// 加密
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.ENCRYPT_MODE, deskey);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	// keybyte为加密密钥，长度为24字节
	// src为加密后的缓冲区
	public static byte[] decryptMode(byte[] keybyte, byte[] src) {
		try {
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
			// 解密
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.DECRYPT_MODE, deskey);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}



	public static void main(String[] args) {
		String key = "2012PinganVitality075522";
		System.out.println("this is key :"+key);
		String szSrc = "{\"serlNum\":\"34192200003296\",\"groupId\":\"8013\",\"transCode\":\"TRS001006\",\"reqTime\":\"2017-03-08 14:55:07\",\"operationCode\":\"18000\",\"authId\":\"\",\"user\":\"8013\",\"mobile\":\"\",\"idType\":\"\",\"id\":\"\",\"accName\":\"李尚\",\"accProvince\":\"北京\",\"accCity\":\"北京\",\"accBank\":\"中国民生银行\",\"accBranchName\":\"中国民生银行厦门文园分理处\",\"accNum\":\"0002900F0345178\",\"accType\":\"00\",\"accProp\":\"PR\",\"amount\":\"496.80\",\"branchId\":\"0\",\"merdata1\":\"\",\"merdata2\":\"\",\"channel\":\"0\",\"accBranchNum\":\"102393000420\",\"totalAmount\":\"496.80\",\"fee\":\"0.00\",\"urgency\":\"U2\",\"amountSplit\":\"1\",\"pin\":\"\",\"channelCode\":\"\",\"url\":\"\"}";
		System.out.println("加密前的字符串:" + szSrc);
//
		byte[] encoded = encryptMode(key.getBytes(), szSrc.getBytes());
		System.out.println("加密后的字符串:" + new String(encoded));
//
		byte[] srcBytes = decryptMode(key.getBytes(), encoded);
		System.out.println("解密后的字符串:" + (new String(srcBytes)));
	}
}
